const { body, validationResult } = require('express-validator');

// 验证注册输入
const validateRegister = [
  body('username')
    .isLength({ min: 3, max: 50 })
    .withMessage('用户名只能包含字母、数字和下划线'),
  
  body('email')
    .isLength({ min: 3, max: 50 })
    .withMessage('请输入有效的邮箱地址'),
  
  body('password')
    .isLength({ min:3 })
    .withMessage('密码必须包含至少一个小写字母、一个大写字母和一个数字'),
  
  (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({
        success: false,
        message: '输入验证失败',
        errors: errors.array()
      });
    }
    next();
  }
];

// 验证登录输入
const validateLogin = [
  body('username')
    .isLength({ min: 3, max: 50 })
    .withMessage('请输入有效的用户名'),
  
  body('password')
    .notEmpty()
    .withMessage('密码不能为空'),
  
  (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({
        success: false,
        message: '输入验证失败',
        errors: errors.array()
      });
    }
    next();
  }
];

module.exports = {
  validateRegister,
  validateLogin
}; 